package com.audaque.springboot.foshanupload.web.lic.controller;


import com.audaque.springboot.foshanupload.web.lic.common.bo.ResultBean;
import com.audaque.springboot.foshanupload.web.lic.common.license.bo.LicenseCheckModel;
import com.audaque.springboot.foshanupload.web.lic.service.LicenseCreatorService;
import com.audaque.springboot.foshanupload.web.lic.service.LicenseVerifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangchen@deepglint.com
 * @desc 用于生成证书文件，不能放在给客户部署的代码里
 * @date 2019/3/14 10:33
 */
@RestController
@RequestMapping("/license")
public class LicenseController {

    @Autowired
    private LicenseCreatorService licenseCreatorService;

    @Autowired
    private LicenseVerifyService licenseVerifyService;

    /**
     * 获取服务器硬件信息
     *
     * @param osName 操作系统类型，如果为空则自动判断
     */
    @RequestMapping(value = "/getServerInfos", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResultBean<?> getServerInfos(@RequestParam(value = "osName", required = false) String osName) {
        LicenseCheckModel licenseCheckModel = licenseCreatorService.getLicenseCheckModel(osName);
        return ResultBean.ok(licenseCheckModel);
    }


    /**
     * 生成授权文件
     */
    @RequestMapping(value = "/generateLicense", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResultBean<?> generateLicense() throws Exception {
        boolean result = licenseCreatorService.generateLicense();
        if (result) {
            return ResultBean.ok("授权文件生成成功！");
        } else {
            return ResultBean.fail("授权文件生成失败！");
        }
    }


    /**
     * 安装授权文件
     */
    @RequestMapping(value = "/install", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResultBean<?> install() throws Exception {
        boolean result1 = licenseVerifyService.install();
        boolean result = licenseVerifyService.verify();

        if (result) {
            return ResultBean.ok("授权文件安装校验成功！");
        } else {
            return ResultBean.fail("授权文件安装校验失败！");
        }
    }

    /**
     * 校验授权文件
     */
    @RequestMapping(value = "/verify", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResultBean<?> verify() throws Exception {
        boolean result = licenseVerifyService.verify();
        if (result) {
            return ResultBean.ok("授权文件校验成功！");
        } else {
            return ResultBean.fail("授权文件校验失败！");
        }
    }


}
